package com.wyz.utils.dingtalk.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RestController
@RequestMapping("/zip")
public class ZipController {

    @PostMapping("")
    public void export(){

        for (int i = 0; i < 3; i++) {
            List<Student> list = new ArrayList<>();

            for (int i1 = 0; i1 < 10; i1++) {
                Student student = new Student("学生"+(i1*100+i),i1*100+i);
                list.add(student);
            }
            EasyExcel.write("/Users/wangzheng/Documents/a"+i+".xlsx", Student.class).sheet().doWrite(list);
        }
        try {
            // 定义要压缩的文件列表
            String[] srcFiles = { "/Users/wangzheng/Documents/a0.xlsx", "/Users/wangzheng/Documents/a1.xlsx", "/Users/wangzheng/Documents/a2.xlsx" };
            // 创建 zip 输出流
            FileOutputStream fos = new FileOutputStream("/Users/wangzheng/Documents/afiles.zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            // 遍历所有文件并将其写入 zip 输出流
            for (String srcFile : srcFiles) {
                File file = new File(srcFile);
                FileInputStream fis = new FileInputStream(file);
                zos.putNextEntry(new ZipEntry(file.getName()));
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
                fis.close();
            }
            zos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


@Data
@AllArgsConstructor
class Student{
    @ExcelProperty("姓名")
    private String name;
    @ExcelProperty("年龄")
    private Integer age;
}